/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
 var isBalanced = function(root) {
    let res=true;
    function def(root){
        if(!root){
            return 0;
        }
        if(!res){
            return ;
        }
        let leftheight=def(root.left)+1;
        let rightheight=def(root.right)+1;
        if(Math.abs(leftheight-rightheight)>1){
            res=false;
        }
        return Math.max(leftheight,rightheight);
    }
    def(root);
    return res;
};